package com.clip.appaction;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.clip.action.util.SessionAttributeKey;
import com.clip.entity.ClipEvent;
import com.clip.entity.ClipeventPic;
import com.clip.entity.Clipmessage;
import com.clip.entity.RequestEvent;
import com.clip.entity.ShareEvent;
import com.clip.entity.Sharemapping;
import com.clip.entity.User;
import com.clip.exception.ClipEventNotExistException;
import com.clip.service.ClipAddressrefService;
import com.clip.service.ClipeventPicService;
import com.clip.service.ContactService;
import com.clip.service.EventService;
import com.clip.service.SystemDateService;
import com.clip.service.UserService;
import com.clip.util.Lunar;
import com.clip.util.MD5Util;
import com.clip.util.Struts2Utils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@Scope("prototype")
public class EventAppAction {
	@Autowired
	private EventService eventService;
	@Autowired
	private UserService userService;
	@Autowired
	private ClipeventPicService picService;
	@Autowired
	private ClipAddressrefService addressrefService;
	@Autowired
	private ContactService contactService;
	@Autowired
	private SystemDateService systemDateService;
	
	private int SUCCESS=10000;
	private int FAILED=10001;
	private int PARAMERROR=10011;
	
	private int COMMENTPAGELENGTH=10;
	private int INTERESTPAGELENGTH=20;
	private int FAVORPAGELENGTH=20;
	private int FORWARDPAGELENGTH=20;
	
	private int SEARCHEVENTPAGELENGTH=20;
	
	private int CALENDAREVENTPAGELENGTH=20;
	
	private int SUBSCRIBEEVENTLISTLENGTH=20;
				
	private String CALENDARQUERYTYPE_PUBLIC="public";
	private String CALENDARQUERYTYPE_PRIVATE="private";
	private String CALENDARQUERYTYPE_MEMO="memo";
	private String CALENDARQUERYTYPE_SHARE="share";
	private String CALENDARQUERYTYPE_OWN="own";
	
	public void submitclip(){
		User currentUser=this.userService.getUserById(uid);
		JSONObject returnObj=new JSONObject();
		if(currentUser==null||currentUser.getStatus()!=1){
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", "当前用户不存在");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}else{
			ClipeventPic clipeventPic=null;
			if(clippic!=null){
				String md5 =null;
				try {
					md5 = MD5Util.getFileMD5String(clippic);
				} catch (IOException e) {
					e.printStackTrace();
					returnObj.put("result", FAILED);
					returnObj.put("errormsg", "图片文件异常");
					Struts2Utils.renderJson(returnObj.toString());
					return;
				}
				if(!md5.equals(md5code)){
					returnObj.put("result", FAILED);
					returnObj.put("errormsg", "图片文件异常");
					Struts2Utils.renderJson(returnObj.toString());
					return;
				}
				clipeventPic=this.picService.saveTempClippic(currentUser.getId(), clippic, clippicfilename);
				if(clipeventPic==null||clipeventPic.getId()==0){
					returnObj.put("result", FAILED);
					returnObj.put("errormsg", "保存图片文件异常");
					Struts2Utils.renderJson(returnObj.toString());
					return;
				}
			}
			ClipEvent saved=null;
			Calendar now=Calendar.getInstance();
			if(this.eventid!=0){
				saved=eventService.updateClipEvent(eventid,cliptype,title,address,content,begintime,endtime,alerttime,0l,currentUser,now,1,mobilenotice,"",iswholeday,sharelist,1);
			}else
				saved=eventService.createClipEvent(cliptype,title,address,content,begintime,endtime,alerttime,0l,currentUser,now,1,mobilenotice,"",iswholeday,share2userlist,1);
			if(saved.getId()>0&&currentUser.getUsertype()==1&&clipeventPic!=null&&clipeventPic.getId()>0){
				this.picService.updateClipeventpic(saved.getId(),clipeventPic.getId());
				if(longitude>0f&&latitude>0f)
					this.addressrefService.saveClipAddressref(saved.getId(), longitude, latitude);
			}
			returnObj.put("result", SUCCESS);
			returnObj.put("eventid", saved.getId());
			if(saved.getShareid()!=null&&saved.getShareid().length()>0){
				returnObj.put("shareid", saved.getShareid());
			}
			Struts2Utils.renderJson(returnObj.toString());
		}
		
	}
	
	public void getDetailEvent(){
		if(uid==0||eventid==0){
			JSONObject returnObj=new JSONObject();
			returnObj.put("result", PARAMERROR);
			returnObj.put("errormsg", "传入参数错误");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}
		User currentUser=this.userService.getUserById(uid);
		JSONObject returnObj=new JSONObject();
		if(currentUser==null||currentUser.getStatus()!=1){
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", "当前用户不存在");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}else{
			ClipEvent clipEvent=this.eventService.getDetailClipEvent(eventid);
			if(clipEvent==null){//||clipEvent.getStatus()!=1
				returnObj.put("result", FAILED);
				returnObj.put("errormsg", "档期不存在或已被删除");
				Struts2Utils.renderJson(returnObj.toString());
				return;
			}
			int reliedclip=clipEvent.getReliedclip();
			ClipEvent detailEvent=null;
			if(reliedclip>0){
				detailEvent=this.eventService.getDetailClipEvent(reliedclip);
				if(detailEvent==null){//||detailEvent.getStatus()!=1
					returnObj=new JSONObject();
					returnObj.put("result", FAILED);
					returnObj.put("errormsg", "源档期已不存在或已被删除");
					Struts2Utils.renderJson(returnObj.toString());
					return;
				}
			}else
				detailEvent=clipEvent;
			Calendar now=Calendar.getInstance();
			returnObj.put("result", SUCCESS);
			returnObj.put("reliedclip",clipEvent.getReliedclip());
			
			returnObj.put("eventid", detailEvent.getId());
			returnObj.put("unameid", detailEvent.getUid());
			returnObj.put("uname", this.contactService.getUserName(currentUser.getId(),detailEvent.getUid()));
			List urllist=this.userService.getUserLogoByUid(detailEvent.getUid());
			returnObj.put("unamebiglogo", urllist.get(0));
			returnObj.put("unamesmalllogo", urllist.get(1));
			if(detailEvent.getEventtype()==2&&detailEvent.getReliedclip()==0&&detailEvent.getSeid()==0){
				returnObj.put("clipiscc", 1);
			}else{
				returnObj.put("clipiscc", 0);
			}
			int fuid=detailEvent.getSeid();
			returnObj.put("seid", fuid);
			if(fuid>0&&detailEvent.getEventtype()==2){
				ClipEvent sourceEvent=this.eventService.getDetailClipEvent(fuid);
				if(sourceEvent==null||sourceEvent.getStatus()!=1){
					returnObj=new JSONObject();
					returnObj.put("result", FAILED);
					returnObj.put("errormsg", "源档期已不存在或已被删除");
					Struts2Utils.renderJson(returnObj.toString());
					return;
				}else{
					
					User sourceUser=this.userService.getUserById(sourceEvent.getUid());
					returnObj.put("sourceeventid", sourceEvent.getId());
					returnObj.put("sourceuid", sourceEvent.getUid());
					returnObj.put("sourceuname", this.contactService.getUserName(currentUser.getId(), sourceUser.getId()));
					List sourceuserlist=this.userService.getUserLogoByUid(sourceUser.getId());
					returnObj.put("sourcesmalllogo", sourceuserlist.get(1));
					returnObj.put("sourcebiglogo", sourceuserlist.get(0));
				}
				
			}
			returnObj.put("eventtype",detailEvent.getEventtype() );
			
			int cliptype=detailEvent.getCliptype();
			
			returnObj.put("title", detailEvent.getTitle());
			returnObj.put("starttime",detailEvent.getBegintime());
			returnObj.put("endtime", detailEvent.getEndtime());
			returnObj.put("iswholeday", detailEvent.getIswholeday());
			returnObj.put("invitetime", detailEvent.getInvitetime());
			returnObj.put("shareid", detailEvent.getShareid());
//			int isself=clipEvent.getUid()==currentUser.getId()?1:0;
//			returnObj.put("isuserself", isself);
			String content=detailEvent.getContent();
			if(content==null){
				content="";
			}
			returnObj.put("content",content);
			//重新判断是否有闹钟
			int result=0;
			if(clipEvent.getUid()==currentUser.getId()){
				returnObj.put("clocktime", clipEvent.getAlerttime());
				returnObj.put("canclock", true);
			}else{
				result=this.eventService.getInviteEventInterestORNOT(detailEvent.getId(), currentUser.getId());
				if(result>0){
					ClipEvent interestedevent=this.eventService.getDetailClipEvent(result);
					returnObj.put("clocktime", interestedevent.getAlerttime());
					returnObj.put("canclock", true);
				}else{
					returnObj.put("canclock", false);
				}
			}
			
//			returnObj.put("processstatus", detailEvent.getProcessstatus());
			
			String picurl=this.picService.getClipeventPic(eventid);
			if(picurl!=null){
				returnObj.put("picurl",picurl);
			}
			returnObj.put("cliptype", cliptype);
			if(cliptype==4){
				List list=this.eventService.getStatAboutClipEvent(clipEvent,currentUser); //得到分享档期的统计
				returnObj.put("commentcount", list.get(0));
			}else if(cliptype==3){
				if(clipEvent.getEventtype()==2&&(clipEvent.getSeid()==0||clipEvent.getReliedclip().equals(clipEvent.getSeid()))){
					//说明用户直接查看商家档期或者是查看直接感兴趣商家的档期
					//需要	感兴趣数		评论数	喜欢数	是否已经感兴趣	是否已经喜欢
					List list=this.eventService.getStatAboutClipEvent(clipEvent,currentUser); //得到分享档期的统计
					returnObj.put("interestcount", list.get(0));
					returnObj.put("forwardcount", list.get(1));
					returnObj.put("favorcount", list.get(2));
					returnObj.put("commentcount", list.get(3));
					List<Long> checklist=this.eventService.checkUserStatusAboutShareEvent(clipEvent,currentUser);
					returnObj.put("isfavor",checklist.get(1)>0l?true:false);
					if(result>0){
						returnObj.put("eventisinterested",true);
					}else{
						returnObj.put("eventisinterested",false);
					}
					List<Integer> uidlist=this.eventService.findHaveInterestFriends(currentUser.getId(), detailEvent.getId());
					if(uidlist!=null&&uidlist.size()>0){
						returnObj.put("size", uidlist.size());
						JSONArray array=new JSONArray();
						for (Integer uid : uidlist) {
							JSONObject temp=new JSONObject();
							List logos=this.userService.getUserLogoByUid(uid);
							String biglogo=(String) logos.get(0);
							String smalllogo=(String) logos.get(1);
							String name=this.contactService.getUserName(currentUser.getId(), uid);
							temp.put("uid",uid);
							temp.put("name",name);
							temp.put("smalllogo",smalllogo);
							temp.put("biglogo",biglogo);
							array.add(temp);
						}
						returnObj.put("users", array);
					}else{
						returnObj.put("size", 0);
					}
					
				}else{
					List list=this.eventService.getStatAboutClipEvent(clipEvent,currentUser); //得到分享档期的统计
					returnObj.put("commentcount", list.get(0));
					boolean isconfirmevent=false;
					isconfirmevent=this.eventService.checkIsInvitedEvent(detailEvent.getId());
					returnObj.put("isconfirmevent", isconfirmevent);
					//检查是否屏蔽
					if(detailEvent.getUid()!=currentUser.getId()||reliedclip>0){
						boolean isshielded=this.eventService.checkIsShielded(detailEvent.getId(), currentUser.getId());
						returnObj.put("isshielded", isshielded);
					}else
						returnObj.put("isshielded", false);
					
					List share2users=null;
					share2users=this.eventService.getShare2users(detailEvent.getId());
					List interestusers=this.eventService.findHaveInterestFriends(currentUser.getId(),detailEvent.getId());
					JSONArray array=new JSONArray();
					List temp1=new ArrayList();//放已感兴趣的
					List temp2=new ArrayList();//放未感兴趣的
					for (Object object : share2users) {
						Sharemapping sharemapping=(Sharemapping) object;
						int uid=sharemapping.getShare2uid();
						if(uid==currentUser.getId())
							continue;
						if(interestusers.contains(uid)){
							temp1.add(uid);
						}else{
							temp2.add(uid);
						}
					}
					for (Object object : temp1) {
						Integer uid=(Integer) object;
						JSONObject temp=new JSONObject();
						List logos=this.userService.getUserLogoByUid(uid);
						String biglogo=(String) logos.get(0);
						String smalllogo=(String) logos.get(1);
						String name=this.contactService.getUserName(currentUser.getId(), uid);
						temp.put("uid",uid);
						temp.put("name",name);
						temp.put("smalllogo",smalllogo);
						temp.put("biglogo",biglogo);
						temp.put("userisinterested", true);
						array.add(temp);
					}
					for (Object object : temp2) {
						Integer uid=(Integer) object;
						JSONObject temp=new JSONObject();
						List logos=this.userService.getUserLogoByUid(uid);
						String biglogo=(String) logos.get(0);
						String smalllogo=(String) logos.get(1);
						String name=this.contactService.getUserName(currentUser.getId(), uid);
						temp.put("uid",uid);
						temp.put("name",name);
						temp.put("smalllogo",smalllogo);
						temp.put("biglogo",biglogo);
						temp.put("userisinterested", false);
						array.add(temp);
					}
					returnObj.put("users", array);
					returnObj.put("size", share2users.size());
				}
				
			}
			
			
			Struts2Utils.renderJson(returnObj.toString());
			
			
		}
	}
	
	public void getEventComments(){
		if(uid==0||eventid==0){
			JSONObject returnObj=new JSONObject();
			returnObj.put("result", PARAMERROR);
			returnObj.put("errormsg", "传入参数错误");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}
		int perpagelength=0;
		if(pagelength!=null&&pagelength>0){
			perpagelength=pagelength;
		}else{
			perpagelength=INTERESTPAGELENGTH;
		}
		User currentUser=this.userService.getUserById(uid);
		JSONObject returnObj=new JSONObject();
		if(currentUser==null||currentUser.getStatus()!=1){
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", "当前用户不存在");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}else{
			returnObj.put("result", SUCCESS);
			JSONArray array=new JSONArray();
			long totalcount=this.eventService.countMessageByEventid(this.eventid);
			BigDecimal br = new BigDecimal(totalcount).divide(new BigDecimal(perpagelength),BigDecimal.ROUND_UP);
			Integer pageCount = (br.doubleValue()<1&&br.doubleValue()>=0 ? 1 : br.intValue());
			if(needpagenum>=pageCount-1){
				returnObj.put("hasmore", 0);
			}else{
				returnObj.put("hasmore", 1);
			}
			List<Clipmessage> list=null;
			list=this.eventService.loadPagingMessageByEventid(this.eventid, needpagenum, perpagelength);
			for (Clipmessage clipmessage : list) {
				JSONObject object=new JSONObject();
				object.put("content", clipmessage.getContent());
				object.put("replytime", clipmessage.getReplytime());
				int uid=clipmessage.getUid();
				object.put("uid", uid);
				String name=this.contactService.getUserName(currentUser.getId(), uid);
				object.put("name", name);
				List logolist=this.userService.getUserLogoByUid(uid);
				object.put("smalllogo", logolist.get(1));
				object.put("biglogo", logolist.get(0));
				object.put("mid", clipmessage.getId());
				array.add(object);
			}
			returnObj.put("comments",array);
		}
		Struts2Utils.renderJson(returnObj.toString());
	}
	
	public void findHaveInterestFriends(){
		User currentUser=this.userService.getUserById(uid);
		JSONObject returnObj=new JSONObject();
		if(currentUser==null||currentUser.getStatus()!=1){
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", "当前用户不存在");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}else{
			ClipEvent clipEvent=this.eventService.getDetailClipEvent(eventid);
			List list=this.eventService.findHaveInterestFriends(currentUser.getId(),eventid);
			returnObj.put("result", SUCCESS);
			JSONArray array=new JSONArray();
			for (Object object : list) {
				Integer uid=(Integer) object;
				JSONObject temp=new JSONObject();
				String smalllogo=(String) this.userService.getUserLogoByUid(uid).get(1);
				String name=this.contactService.getUserName(currentUser.getId(), uid);
				temp.put("uid",uid);
				temp.put("name",name);
				temp.put("smalllogo",smalllogo);
				array.add(temp);
			}
			returnObj.put("users", array);
		}
		Struts2Utils.renderJson(returnObj.toString());
	}
	
	public void getEventInterests(){
		if(uid==0||eventid==0){
			JSONObject returnObj=new JSONObject();
			returnObj.put("result", PARAMERROR);
			returnObj.put("errormsg", "传入参数错误");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}
		
		int perpagelength=0;
		if(pagelength!=null&&pagelength>0){
			perpagelength=pagelength;
		}else{
			perpagelength=INTERESTPAGELENGTH;
		}
		User currentUser=this.userService.getUserById(uid);
		JSONObject returnObj=new JSONObject();
		if(currentUser==null||currentUser.getStatus()!=1){
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", "当前用户不存在");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}else{
			long totalcount=this.eventService.countEventInterests(currentUser.getId(), eventid);
			BigDecimal br = new BigDecimal(totalcount).divide(new BigDecimal(perpagelength),BigDecimal.ROUND_UP);
			Integer pageCount = (br.doubleValue()<1&&br.doubleValue()>=0 ? 1 : br.intValue());
			List list=null;
			list=this.eventService.getEventInterests(currentUser.getId(),eventid,needpagenum,perpagelength);
			if(list==null){
				returnObj.put("result", FAILED);
				returnObj.put("errormsg", "不允许查看当前档期");
				Struts2Utils.renderJson(returnObj.toString());
				return;
			}else{
				returnObj.put("result", SUCCESS);
				if(needpagenum>=pageCount-1){
					returnObj.put("hasmore", 0);
				}else{
					returnObj.put("hasmore", 1);
				}
				JSONArray array=new JSONArray();
				for (Object object : list) {
					Integer uid=(Integer) object;
					JSONObject temp=new JSONObject();
					List logolist=this.userService.getUserLogoByUid(uid);
					String name=this.contactService.getUserName(currentUser.getId(), uid);
					temp.put("uid",uid);
					temp.put("name",name);
					temp.put("smalllogo",logolist.get(1));
					temp.put("biglogo",logolist.get(0));
					array.add(temp);
				}
				returnObj.put("users", array);
			}
		}
		Struts2Utils.renderJson(returnObj.toString());
	}
	
	public void getEventFavors(){
		if(uid==0||eventid==0){
			JSONObject returnObj=new JSONObject();
			returnObj.put("result", PARAMERROR);
			returnObj.put("errormsg", "传入参数错误");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}
		int perpagelength=0;
		if(pagelength!=null&&pagelength>0){
			perpagelength=pagelength;
		}else{
			perpagelength=INTERESTPAGELENGTH;
		}
		User currentUser=this.userService.getUserById(uid);
		JSONObject returnObj=new JSONObject();
		if(currentUser==null||currentUser.getStatus()!=1){
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", "当前用户不存在");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}else{
			long totalcount=this.eventService.countEventFavors(currentUser.getId(),eventid);
			List list=null;
			list=this.eventService.getEventFavors(currentUser.getId(),eventid,needpagenum,perpagelength);
			BigDecimal br = new BigDecimal(totalcount).divide(new BigDecimal(perpagelength),BigDecimal.ROUND_UP);
			Integer pageCount = (br.doubleValue()<1&&br.doubleValue()>=0 ? 1 : br.intValue());
			if(list==null){
				returnObj.put("result", FAILED);
				returnObj.put("errormsg", "不允许查看当前档期");
				Struts2Utils.renderJson(returnObj.toString());
				return;
			}else{
				returnObj.put("result", SUCCESS);
				if(needpagenum>=pageCount-1){
					returnObj.put("hasmore", 0);
				}else{
					returnObj.put("hasmore", 1);
				}
				JSONArray array=new JSONArray();
				for (Object object : list) {
					Integer uid=(Integer) object;
					JSONObject temp=new JSONObject();
					List logolist=this.userService.getUserLogoByUid(uid);
					String name=this.contactService.getUserName(currentUser.getId(), uid);
					temp.put("uid",uid);
					temp.put("name",name);
					temp.put("smalllogo",logolist.get(1));
					temp.put("biglogo",logolist.get(0));
					array.add(temp);
				}
				returnObj.put("users", array);
			}
		}
		Struts2Utils.renderJson(returnObj.toString());
	}
	
	public void getEventForwards(){
		if(uid==0||eventid==0){
			JSONObject returnObj=new JSONObject();
			returnObj.put("result", PARAMERROR);
			returnObj.put("errormsg", "传入参数错误");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}
		User currentUser=this.userService.getUserById(uid);
		JSONObject returnObj=new JSONObject();
		if(currentUser==null||currentUser.getStatus()!=1){
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", "当前用户不存在");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}else{
			List list=this.eventService.getEventForwards(currentUser.getId(),eventid,needpagenum,this.FORWARDPAGELENGTH);
			returnObj.put("result", SUCCESS);
			JSONArray array=new JSONArray();
			for (Object object : list) {
				Integer uid=(Integer) object;
				JSONObject temp=new JSONObject();
				String smalllogo=(String) this.userService.getUserLogoByUid(uid).get(1);
				String name=this.contactService.getUserName(currentUser.getId(), uid);
				temp.put("uid",uid);
				temp.put("name",name);
				temp.put("smalllogo",smalllogo);
				array.add(temp);
			}
			returnObj.put("users", array);
		}
		Struts2Utils.renderJson(returnObj.toString());
	}
	
	public void searchEvent(){
		if(uid==0||searchkey==null||searchkey.length()<=0){
			JSONObject returnObj=new JSONObject();
			returnObj.put("result", PARAMERROR);
			returnObj.put("errormsg", "传入参数错误");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}
		User currentUser=this.userService.getUserById(uid);
		JSONObject returnObj=new JSONObject();
		if(currentUser==null||currentUser.getStatus()!=1){
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", "当前用户不存在");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}else{
			returnObj.put("result", SUCCESS);
			int perpagelength=0;
			if(this.pagelength==0){
				perpagelength=this.SEARCHEVENTPAGELENGTH;
			}else{
				perpagelength=this.pagelength;
			}
			Long searchcount=this.eventService.queryClipListTotalCount("search",currentUser,searchkey,null);
			returnObj.put("searchcount", searchcount);
			BigDecimal br = new BigDecimal(searchcount).divide(new BigDecimal(perpagelength),BigDecimal.ROUND_UP);
			Integer pageCount = (br.doubleValue()<1&&br.doubleValue()>=0 ? 1 : br.intValue());
			if(needpagenum>=pageCount-1){
				returnObj.put("hasmore", 0);
			}else{
				returnObj.put("hasmore", 1);
			}
			List list=this.eventService.searchEvent(searchkey,currentUser,needpagenum,perpagelength);
			JSONArray array=new JSONArray();
			for (Object object : list) {
				JSONObject jsonObject=new JSONObject();
				ClipEvent clipevent=(ClipEvent) object;
				int eventid=clipevent.getId();
				String title=clipevent.getTitle();
				int uid=clipevent.getUid();
				long starttime=clipevent.getBegintime();
				long endtime=clipevent.getEndtime();
				int iswholeday=clipevent.getIswholeday();
				int eventtype=clipevent.getEventtype();
				jsonObject.put("eventid", eventid);
				jsonObject.put("title", title);
				jsonObject.put("uid", uid);
				jsonObject.put("starttime", starttime);
				jsonObject.put("endtime",endtime);
				jsonObject.put("iswholeday", iswholeday);
				
				jsonObject.put("unameid", uid);
				jsonObject.put("uname", this.contactService.getUserName(currentUser.getId(),uid));
				List urllist=this.userService.getUserLogoByUid(uid);
				jsonObject.put("unamebiglogo", urllist.get(0));
				jsonObject.put("unamesmalllogo", urllist.get(1));
				jsonObject.put("eventtype",eventtype);
				jsonObject.put("clipusertype", clipevent.getEventtype());
				array.add(jsonObject);
			}
			returnObj.put("events", array);
			Struts2Utils.renderJson(returnObj.toString());
		}
	}
	
	public void favorEvent(){
		if(uid==0||eventid==0){
			JSONObject returnObj=new JSONObject();
			returnObj.put("result", PARAMERROR);
			returnObj.put("errormsg", "传入参数错误");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}
		User currentUser=this.userService.getUserById(uid);
		JSONObject returnObj=new JSONObject();
		if(currentUser==null||currentUser.getStatus()!=1){
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", "当前用户不存在");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}else{
			this.eventService.doFavorOnEvent(currentUser,eventid,isfavor);
			returnObj.put("result", SUCCESS);
			Struts2Utils.renderJson(returnObj.toString());
		}
	}
	
	public void interestEvent(){
		if(uid==0||eventid==0){
			JSONObject returnObj=new JSONObject();
			returnObj.put("result", PARAMERROR);
			returnObj.put("errormsg", "传入参数错误");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}
		User currentUser=this.userService.getUserById(uid);
		JSONObject returnObj=new JSONObject();
		if(currentUser==null||currentUser.getStatus()!=1){
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", "当前用户不存在");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}else{
			try {
				ClipEvent userEvent=this.eventService.interestShareEvent(eventid,currentUser);
				returnObj.put("result", SUCCESS);
				returnObj.put("interesteventid", userEvent.getId());
			} catch (ClipEventNotExistException e) {
				returnObj.put("result", FAILED);
				returnObj.put("errormsg", "档期不存在或已被删除");
			}
			Struts2Utils.renderJson(returnObj.toString());
		}
	}
	
	/**
	 * 呼朋唤友
	 */
	public void forwardEvent(){
		if(uid==0||eventid==0||(share2userlist==null||share2userlist.length()==0)){
			JSONObject returnObj=new JSONObject();
			returnObj.put("result", PARAMERROR);
			returnObj.put("errormsg", "传入参数错误");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}
		User currentUser=this.userService.getUserById(uid);
		JSONObject returnObj=new JSONObject();
		if(currentUser==null||currentUser.getStatus()!=1){
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", "当前用户不存在");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}else{
			try {
				ClipEvent shareEvent=this.eventService.forwardShareEvent(eventid, currentUser, share2userlist,null,invitetime);
//				if(shareid==null){
//					returnObj.put("result", FAILED);
//					returnObj.put("errormsg", "操作错误");
//				}else{
				returnObj.put("result", SUCCESS);
				returnObj.put("shareid", shareEvent.getShareid());
//				}
			} catch (ClipEventNotExistException e) {
				returnObj.put("result", FAILED);
				returnObj.put("errormsg", "档期不存在或已被删除");
			}
			Struts2Utils.renderJson(returnObj.toString());
		}
	}
	
	public void cancelInterest(){
		if(uid==0||eventid==0){
			JSONObject returnObj=new JSONObject();
			returnObj.put("result", PARAMERROR);
			returnObj.put("errormsg", "传入参数错误");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}
		User currentUser=this.userService.getUserById(uid);
		JSONObject returnObj=new JSONObject();
		if(currentUser==null||currentUser.getStatus()!=1){
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", "当前用户不存在");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}else{
			try {
				this.eventService.cancelInterest(eventid,currentUser);
				returnObj.put("result", SUCCESS);
			} catch (ClipEventNotExistException e) {
				returnObj.put("result", FAILED);
				returnObj.put("errormsg", "档期不存在或已被删除");
			}
			Struts2Utils.renderJson(returnObj.toString());
		}
	}
	
	public void commentEvent(){
		if(uid==0||eventid==0||comment==null||comment.length()==0){
			JSONObject returnObj=new JSONObject();
			returnObj.put("result", PARAMERROR);
			returnObj.put("errormsg", "传入参数错误");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}
		User currentUser=this.userService.getUserById(uid);
		JSONObject returnObj=new JSONObject();
		if(currentUser==null||currentUser.getStatus()!=1){
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", "当前用户不存在");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}else{
			String result=this.eventService.replyMessage(eventid,comment,currentUser,Calendar.getInstance());
			if(result!=null){
				returnObj.put("result", FAILED);
				returnObj.put("errormsg", result);
			}else{
				returnObj.put("result", SUCCESS);
			}
			Struts2Utils.renderJson(returnObj.toString());
		}
	}
	
	public void addClock(){
		if(uid==0||eventid==0||alerttime==0l){
			JSONObject returnObj=new JSONObject();
			returnObj.put("result", PARAMERROR);
			returnObj.put("errormsg", "传入参数错误");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}
		User currentUser=this.userService.getUserById(uid);
		JSONObject returnObj=new JSONObject();
		if(currentUser==null||currentUser.getStatus()!=1){
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", "当前用户不存在");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}else{
			String re=this.eventService.app_AddClock(currentUser.getId(),eventid, alerttime);
			if(re==null){
				returnObj.put("result", SUCCESS);
			}else{
				returnObj.put("result", FAILED);
				returnObj.put("errormsg",re);
			}
			Struts2Utils.renderJson(returnObj.toString());
		}
	}
	
	public void cancelClock(){
		if(uid==0||eventid==0){
			JSONObject returnObj=new JSONObject();
			returnObj.put("result", PARAMERROR);
			returnObj.put("errormsg", "传入参数错误");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}
		User currentUser=this.userService.getUserById(uid);
		JSONObject returnObj=new JSONObject();
		if(currentUser==null||currentUser.getStatus()!=1){
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", "当前用户不存在");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}else{
			String re=this.eventService.cancelClock(currentUser, eventid);
			if(re==null){
				returnObj.put("result", SUCCESS);
			}else{
				returnObj.put("result", FAILED);
				returnObj.put("errormsg",re);
			}
			Struts2Utils.renderJson(returnObj.toString());
		}
	}
	
//	public void acceptInvite(){
//		if(uid==0||eventid==0){
//			JSONObject returnObj=new JSONObject();
//			returnObj.put("result", PARAMERROR);
//			returnObj.put("errormsg", "传入参数错误");
//			Struts2Utils.renderJson(returnObj.toString());
//			return;
//		}
//		User currentUser=this.userService.getUserById(uid);
//		JSONObject returnObj=new JSONObject();
//		if(currentUser==null||currentUser.getStatus()!=1){
//			returnObj.put("result", FAILED);
//			returnObj.put("errormsg", "当前用户不存在");
//			Struts2Utils.renderJson(returnObj.toString());
//			return;
//		}else{
//			ClipEvent clipEvent=this.eventService.acceptInvitedClip(currentUser, eventid);
//			if(clipEvent==null){
//				returnObj.put("result", FAILED);
//				returnObj.put("errormsg", "档期不存在或已被删除");
//			}else{
//				returnObj.put("result", SUCCESS);
//			}
//			Struts2Utils.renderJson(returnObj.toString());
//		}
//	}
	
	/**
	 * 档期发起者一拍即合档期
	 */
	public void confirmEvent(){
		if(uid==0||eventid==0){
			JSONObject returnObj=new JSONObject();
			returnObj.put("result", PARAMERROR);
			returnObj.put("errormsg", "传入参数错误");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}
		User currentUser=this.userService.getUserById(uid);
		JSONObject returnObj=new JSONObject();
		if(currentUser==null||currentUser.getStatus()!=1){
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", "当前用户不存在");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}else{
			boolean result=this.eventService.confirmEvent(eventid,currentUser,share2userlist);
			if(result){
				returnObj.put("result", SUCCESS);
			}else{
				returnObj.put("result", FAILED);
				returnObj.put("errormsg", "无权操作或档期不存在");
			}
			Struts2Utils.renderJson(returnObj.toString());
		}
		
	}
	
	/**
	 * 退出一拍即合的档期
	 */
	public void refuseInvite(){
		if(uid==0||eventid==0){
			JSONObject returnObj=new JSONObject();
			returnObj.put("result", PARAMERROR);
			returnObj.put("errormsg", "传入参数错误");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}
		User currentUser=this.userService.getUserById(uid);
		JSONObject returnObj=new JSONObject();
		if(currentUser==null||currentUser.getStatus()!=1){
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", "当前用户不存在");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}else{
			ClipEvent clipEvent=this.eventService.refuseInvitedClip(eventid, currentUser);
			if(clipEvent==null){
				returnObj.put("result", FAILED);
				returnObj.put("errormsg", "档期不存在或已被删除");
			}else{
				returnObj.put("result", SUCCESS);
			}
			Struts2Utils.renderJson(returnObj.toString());
		}
	}
	
	/**
	 * 自己发起的档期/收到邀请的档期，才使用删除档期
	 */
	public void deleteEvent(){
		if(uid==0||eventid==0){
			JSONObject returnObj=new JSONObject();
			returnObj.put("result", PARAMERROR);
			returnObj.put("errormsg", "传入参数错误");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}
		User currentUser=this.userService.getUserById(uid);
		JSONObject returnObj=new JSONObject();
		if(currentUser==null||currentUser.getStatus()!=1){
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", "当前用户不存在");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}else{
			String msg=this.eventService.deleteEvent(currentUser, eventid);
			if(msg==null){
				returnObj.put("result", SUCCESS);
			}else{
				returnObj.put("result", FAILED);
				returnObj.put("errormsg",msg);
			}
			Struts2Utils.renderJson(returnObj.toString());
		}
	}
	
	public void getEventsByDay(){
		if(uid==0||month>11||month<0||year==0||date<1||date>31||querytype==null){
			JSONObject returnObj=new JSONObject();
			returnObj.put("result", PARAMERROR);
			returnObj.put("errormsg", "传入参数错误");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}
		boolean hassetlength=false;
		int perpagelength=0;
		if(pagelength!=null&&pagelength>0){
			perpagelength=this.pagelength;
		}else{
			perpagelength=this.CALENDAREVENTPAGELENGTH;
		}
		User currentUser=this.userService.getUserById(uid);
		JSONObject returnObj=new JSONObject();
		if(currentUser==null||currentUser.getStatus()!=1){
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", "当前用户不存在");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}else{
			Calendar now=Calendar.getInstance();
			List list=null;
			long totolcount=this.eventService.countsEventsByDay(year,month,date,currentUser.getId(),querytype);
			list=this.eventService.getEventsByDay(year, month, date, now, currentUser,querytype,needpagenum,perpagelength);
			
			if(list!=null){
				returnObj.put("result", SUCCESS);
				BigDecimal br = new BigDecimal(totolcount).divide(new BigDecimal(perpagelength),BigDecimal.ROUND_UP);
				Integer pageCount = (br.doubleValue()<1&&br.doubleValue()>=0 ? 1 : br.intValue());
				if(needpagenum>=pageCount-1){
					returnObj.put("hasmore", 0);
				}else{
					returnObj.put("hasmore", 1);
				}
				//[public=ClipEvent,private=ClipEvent,memo=ClipEvent,share=ShareEvent]
				JSONArray array=new JSONArray();
				if(querytype.equals(this.CALENDARQUERYTYPE_PUBLIC)){
					for (Object object : list) {
						ClipEvent clipEvent=(ClipEvent) object;
						JSONObject temp=new JSONObject();
						temp.put("eventid", clipEvent.getId());
						temp.put("title", clipEvent.getTitle());
						temp.put("name", this.contactService.getUserName(currentUser.getId(), clipEvent.getUid()));
						List logolist=this.userService.getUserLogoByUid(clipEvent.getUid());
						temp.put("smalllogo", logolist.get(1));
						temp.put("biglogo", logolist.get(0));
						temp.put("reliedclip", clipEvent.getReliedclip());
						temp.put("starttime", clipEvent.getBegintime());
						temp.put("endtime", clipEvent.getEndtime());
						temp.put("iswholeday", clipEvent.getIswholeday());
						array.add(temp);
					}
				}else if(querytype.equals(this.CALENDARQUERYTYPE_PRIVATE)){
					for (Object object : list) {
						ClipEvent clipEvent=(ClipEvent) object;
						JSONObject temp=new JSONObject();
						temp.put("eventid", clipEvent.getId());
						temp.put("title", clipEvent.getTitle());
						temp.put("name", this.contactService.getUserName(currentUser.getId(), clipEvent.getUid()));
						List logolist=this.userService.getUserLogoByUid(clipEvent.getUid());
						temp.put("smalllogo", logolist.get(1));
						temp.put("biglogo", logolist.get(0));
						temp.put("reliedclip", clipEvent.getReliedclip());
						temp.put("starttime", clipEvent.getBegintime());
						temp.put("endtime", clipEvent.getEndtime());
						temp.put("iswholeday", clipEvent.getIswholeday());
						array.add(temp);
					}
				}else if(querytype.equals(this.CALENDARQUERYTYPE_MEMO)){
					for (Object object : list) {
						ClipEvent clipEvent=(ClipEvent) object;
						JSONObject temp=new JSONObject();
						temp.put("eventid", clipEvent.getId());
						temp.put("title", clipEvent.getTitle());
						temp.put("name", this.contactService.getUserName(currentUser.getId(), clipEvent.getUid()));
						List logolist=this.userService.getUserLogoByUid(clipEvent.getUid());
						temp.put("smalllogo", logolist.get(1));
						temp.put("biglogo", logolist.get(0));
						temp.put("starttime", clipEvent.getBegintime());
						temp.put("endtime", clipEvent.getEndtime());
						array.add(temp);
					}
				}else if(querytype.equals(this.CALENDARQUERYTYPE_OWN)){
					for (Object object : list) {
						ClipEvent clipEvent=(ClipEvent) object;
						ClipEvent toJSONEvent=null;
						if(clipEvent.getReliedclip()>0){
							toJSONEvent=this.eventService.getClipEventById(clipEvent.getId());
						}else{
							toJSONEvent=clipEvent;
						}
						
						JSONObject temp=new JSONObject();
						temp.put("eventid", toJSONEvent.getId());
						temp.put("title", toJSONEvent.getTitle());
						temp.put("unameid", toJSONEvent.getUid());
						temp.put("uname", this.contactService.getUserName(currentUser.getId(), toJSONEvent.getUid()));
						List logolist=this.userService.getUserLogoByUid(toJSONEvent.getUid());
						temp.put("unamesmalllogo", logolist.get(1));
						temp.put("unamebiglogo", logolist.get(0));
						temp.put("reliedclip", clipEvent.getReliedclip());
						temp.put("starttime", toJSONEvent.getBegintime());
						temp.put("endtime", toJSONEvent.getEndtime());
						temp.put("iswholeday", toJSONEvent.getIswholeday());
						temp.put("eventtype", toJSONEvent.getEventtype());
//						temp.put("invitetime", clipEvent.getInvitetime());
						array.add(temp);
					}
				}else if(querytype.equals(this.CALENDARQUERYTYPE_SHARE)){
					for (Object object : list) {
						RequestEvent requestEvent=(RequestEvent) object;
						JSONObject temp=new JSONObject();
						temp.put("eventid", requestEvent.getEventid());
						temp.put("title", requestEvent.getTitle());
						temp.put("unameid", requestEvent.getUid());
						temp.put("uname", this.contactService.getUserName(currentUser.getId(), requestEvent.getUid()));
						List logolist=this.userService.getUserLogoByUid(requestEvent.getUid());
						temp.put("unamesmalllogo", logolist.get(1));
						temp.put("unamebiglogo", logolist.get(0));
						temp.put("reliedclip", requestEvent.getReliedclip());
						temp.put("starttime", requestEvent.getBegintime());
						temp.put("endtime", requestEvent.getEndtime());
						temp.put("iswholeday", requestEvent.getIswholeday());
						temp.put("eventtype", requestEvent.getEventtype());
						array.add(temp);
					}
				}
				returnObj.put("events", array);
			}else{
				returnObj.put("result", FAILED);
				returnObj.put("errormsg", "数据错误");
			}
			Struts2Utils.renderJson(returnObj.toString());
		}
	}
	
	public void loadSubscribeEvents(){
		if(uid==0||month>11||month<0||year==0||date<1||date>31){
			JSONObject returnObj=new JSONObject();
			returnObj.put("result", PARAMERROR);
			returnObj.put("errormsg", "传入参数错误");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}
		int perpagelength=0;
		if(pagelength!=null&&pagelength>0){
			perpagelength=pagelength;
		}else{
			perpagelength=CALENDAREVENTPAGELENGTH;
		}
		User currentUser=this.userService.getUserById(uid);
		JSONObject returnObj=new JSONObject();
		if(currentUser==null||currentUser.getStatus()!=1){
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", "当前用户不存在");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}
		Calendar firstDate=Calendar.getInstance();
		firstDate.clear(Calendar.MILLISECOND);
		firstDate.clear(Calendar.SECOND);
		firstDate.clear(Calendar.MINUTE);
		firstDate.set(Calendar.HOUR_OF_DAY, 0);
		firstDate.set(Calendar.YEAR, year);
		firstDate.set(Calendar.MONTH, month);
		firstDate.set(Calendar.DAY_OF_MONTH,date);
		long totalcount=this.eventService.countAllSubscribeEvents(currentUser, firstDate);
		BigDecimal br = new BigDecimal(totalcount).divide(new BigDecimal(perpagelength),BigDecimal.ROUND_UP);
		Integer pageCount = (br.doubleValue()<1&&br.doubleValue()>=0 ? 1 : br.intValue());
		if(needpagenum>=pageCount-1){
			returnObj.put("hasmore", 0);
		}else{
			returnObj.put("hasmore", 1);
		}
		List list=null;
		list=this.eventService.getAllSubscribeEvents(currentUser,firstDate,needpagenum,perpagelength);
		Calendar now=Calendar.getInstance();
		returnObj.put("result", SUCCESS);
		JSONArray array=new JSONArray();
		for (Object object : list) {
			ShareEvent shareEvent=(ShareEvent) object;
			JSONObject temp=new JSONObject();
			temp.put("eventid", shareEvent.getEventid());
			temp.put("title", shareEvent.getTitle());
			temp.put("name", this.contactService.getUserName(currentUser.getId(), shareEvent.getUid()));
			List logolist=this.userService.getUserLogoByUid(shareEvent.getUid());
			temp.put("biglogo", logolist.get(0));
			temp.put("smalllogo", logolist.get(1));
			temp.put("reliedclip", shareEvent.getReliedclip());
			temp.put("starttime", shareEvent.getBegintime());
			temp.put("endtime", shareEvent.getEndtime());
			temp.put("iswholeday", shareEvent.getIswholeday());
			array.add(temp);
		}
		returnObj.put("events", array);
		Struts2Utils.renderJson(returnObj.toString());
	}
	
	public void getEventssignByMonth(){
		if(uid==0||month>11||month<0||year==0||querytype==null){
			JSONObject returnObj=new JSONObject();
			returnObj.put("result", PARAMERROR);
			returnObj.put("errormsg", "传入参数错误");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}
		User currentUser=this.userService.getUserById(uid);
		JSONObject returnObj=new JSONObject();
		if(currentUser==null||currentUser.getStatus()!=1){
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", "当前用户不存在");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}else{
			returnObj.put("result", SUCCESS);
			JSONArray array=new JSONArray();
			Calendar now=Calendar.getInstance();
			Calendar firstDateOfMonth=Calendar.getInstance();
			firstDateOfMonth.clear(Calendar.MILLISECOND);
			firstDateOfMonth.clear(Calendar.SECOND);
			firstDateOfMonth.clear(Calendar.MINUTE);
			firstDateOfMonth.set(Calendar.HOUR_OF_DAY, 0);
			firstDateOfMonth.set(Calendar.YEAR, year);
			firstDateOfMonth.set(Calendar.MONTH,month);
			firstDateOfMonth.set(Calendar.DAY_OF_MONTH, 1);
			
			Calendar startdate=(Calendar) firstDateOfMonth.clone();
			startdate.add(Calendar.DAY_OF_MONTH, Calendar.SUNDAY-startdate.get(Calendar.DAY_OF_WEEK));
			
			Calendar lastDateOfMonth=(Calendar) firstDateOfMonth.clone();
			lastDateOfMonth.set(Calendar.DAY_OF_MONTH,firstDateOfMonth.getActualMaximum(Calendar.DAY_OF_MONTH));
			
			Calendar lastDate=(Calendar) lastDateOfMonth.clone();
			lastDate.add(Calendar.DAY_OF_MONTH, Calendar.SATURDAY-lastDate.get(Calendar.DAY_OF_WEEK));
			int holiday=0;
			boolean isssmonth=false;
			if(querytype.equals("subscribe")){
				//判断修改
				isssmonth=true;
				returnObj.put("isssmonth", true);
			}else{
				returnObj.put("isssmonth", false);
			}
			while(!startdate.after(lastDate)){
				JSONObject object=new JSONObject();
				int isholiday=isSysHoliday(startdate.get(Calendar.YEAR), startdate.get(Calendar.MONTH), startdate.get(Calendar.DAY_OF_MONTH));
				int weeknum=startdate.get(Calendar.DAY_OF_WEEK);
				boolean isweekend=weeknum==1||weeknum==7?true:false;
				boolean isThisMonth=startdate.get(Calendar.MONTH)==this.month;
				
				Map<String,Object> temp2=new HashMap<String,Object>();
				object.put("isThisMonth", isThisMonth);
				if(isssmonth){
					int size1=this.eventService.countStartedSubscribeByDay(startdate,currentUser.getId(),ids);
					int size2=this.eventService.countEndedSubscribeByDay(startdate,currentUser.getId(),ids);
					object.put("startsharesize", size1);
					object.put("endsharesize", size2);
				}else{
					int count=this.eventService.countsEventsByDay(startdate.get(Calendar.YEAR), startdate.get(Calendar.MONTH), startdate.get(Calendar.DAY_OF_MONTH), currentUser.getId(), this.querytype);
					object.put("eventsize", count);
				}
				object.put("weeknum", startdate.get(Calendar.WEEK_OF_YEAR));
				object.put("weekofmonth", startdate.get(Calendar.WEEK_OF_MONTH));
				object.put("weekday", startdate.get(Calendar.DAY_OF_WEEK));
				object.put("lunartxt", Lunar.getLunarText(startdate));
				SimpleDateFormat dateFormat=new SimpleDateFormat("MMdd");
				object.put("date", dateFormat.format(startdate.getTime()));
				if(isweekend){
					if(isholiday==1||isholiday==-1){ //周末也是节日
						object.put("holiday", true);
					}else{ //周末不休息
						object.put("holiday",false);
					}
				}else{
					if(isholiday==1){ //平时但是节日
						object.put("holiday", true);
					}else{ //平时也不是节日
						object.put("holiday",false);
					}
				}
				startdate.add(Calendar.DAY_OF_MONTH, 1);
				array.add(object);
			}
			returnObj.put("month", array);
			Struts2Utils.renderJson(returnObj.toString());
		}
	}
	
	private int isSysHoliday(int year, int month, int day) {
		return systemDateService.getSystemHolidayByDay(year, month, day);
	}
	
	public void addInterestsInvite(){
		User currentUser=this.userService.getUserById(uid);
		JSONObject returnObj=new JSONObject();
		if(currentUser==null||currentUser.getStatus()!=1){
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", "当前用户不存在");
			Struts2Utils.renderJson(returnObj.toString());
		}else{
			int eventid=this.eventService.addInterestsInvite(currentUser, seid,alerttime,mobilenotice,deadlinetime,contactlist);
			if(eventid>0){
				returnObj.put("result", SUCCESS);
				returnObj.put("eventid", eventid);
				Struts2Utils.renderJson(returnObj.toString());
			}else{
				returnObj.put("result", FAILED);
				returnObj.put("errormsg", "当前用户不存在");
				Struts2Utils.renderJson(returnObj.toString());
			}
		}
	}
	
//	public void launchInviteclipFromShare(){
//		if(uid==0||eventid==0||contactlist==null||contactlist.length()==0){
//			JSONObject returnObj=new JSONObject();
//			returnObj.put("result", PARAMERROR);
//			returnObj.put("errormsg", "传入参数错误");
//			Struts2Utils.renderJson(returnObj.toString());
//			return;
//		}
//		User currentUser=this.userService.getUserById(uid);
//		JSONObject returnObj=new JSONObject();
//		if(currentUser==null||currentUser.getStatus()!=1){
//			returnObj.put("result", FAILED);
//			returnObj.put("errormsg", "当前用户不存在");
//			Struts2Utils.renderJson(returnObj.toString());
//			return;
//		}else{
//			ClipEvent clipevent=this.eventService.launchInviteclipFromShare(currentUser,eventid,contactlist,alerttime,deadlinetime);
//			if(clipevent!=null){
//				returnObj.put("result", SUCCESS);
//				returnObj.put("eventid",clipevent.getId());
//				Struts2Utils.renderJson(returnObj.toString());
//				return;
//			}else{
//				returnObj.put("result", FAILED);
//				returnObj.put("errormsg","档期不存在或已被删除");
//				Struts2Utils.renderJson(returnObj.toString());
//				return;
//			}
//		}
//	}
	
	public void getSubscribedeventsByMonth(){
		if(uid==0||month>11||month<0||year==0){
			JSONObject returnObj=new JSONObject();
			returnObj.put("result", PARAMERROR);
			returnObj.put("errormsg", "传入参数错误");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}
		User currentUser=this.userService.getUserById(uid);
		JSONObject returnObj=new JSONObject();
		if(currentUser==null||currentUser.getStatus()!=1){
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", "当前用户不存在");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}else{
			returnObj.put("result", SUCCESS);
			JSONArray array=new JSONArray();
			Calendar now=Calendar.getInstance();
			Calendar firstDateOfMonth=Calendar.getInstance();
			firstDateOfMonth.clear(Calendar.MILLISECOND);
			firstDateOfMonth.clear(Calendar.SECOND);
			firstDateOfMonth.clear(Calendar.MINUTE);
			firstDateOfMonth.set(Calendar.HOUR_OF_DAY, 0);
			firstDateOfMonth.set(Calendar.YEAR, year);
			firstDateOfMonth.set(Calendar.MONTH,month);
			firstDateOfMonth.set(Calendar.DAY_OF_MONTH, 1);
			
			Calendar startdate=(Calendar) firstDateOfMonth.clone();
			startdate.add(Calendar.DAY_OF_MONTH, Calendar.SUNDAY-startdate.get(Calendar.DAY_OF_WEEK));
			
			Calendar lastDateOfMonth=(Calendar) firstDateOfMonth.clone();
			lastDateOfMonth.set(Calendar.DAY_OF_MONTH,firstDateOfMonth.getActualMaximum(Calendar.DAY_OF_MONTH));
			
			Calendar lastDate=(Calendar) lastDateOfMonth.clone();
			lastDate.add(Calendar.DAY_OF_MONTH, Calendar.SATURDAY-lastDate.get(Calendar.DAY_OF_WEEK));
			int holiday=0;
			while(!startdate.after(lastDate)){
				JSONObject object=new JSONObject();
				int weeknum=startdate.get(Calendar.DAY_OF_WEEK);
				boolean isweekend=weeknum==1||weeknum==7?true:false;
				boolean isThisMonth=startdate.get(Calendar.MONTH)==this.month;
				
				Map<String,Object> temp2=new HashMap<String,Object>();
				object.put("isThisMonth", isThisMonth);
//				if(isLoadSSAll==1){
				int eventsize=this.eventService.countSubscribeByDay(currentUser,startdate);
				object.put("eventsize", eventsize);
//				}
				object.put("weeknum", startdate.get(Calendar.WEEK_OF_YEAR));
				object.put("weekofmonth", startdate.get(Calendar.WEEK_OF_MONTH));
				object.put("weekday", startdate.get(Calendar.DAY_OF_WEEK));
//				object.put("lunartxt", Lunar.getLunarText(startdate));
				SimpleDateFormat dateFormat=new SimpleDateFormat("MMdd");
				object.put("date", dateFormat.format(startdate.getTime()));
				startdate.add(Calendar.DAY_OF_MONTH, 1);
				array.add(object);
			}
			returnObj.put("month", array);
			Struts2Utils.renderJson(returnObj.toString());
		}
	}
	
	public void loadUsersShareByMonth(){
		if(uid==0||month>11||month<0||year==0||vuid==0){
			JSONObject returnObj=new JSONObject();
			returnObj.put("result", PARAMERROR);
			returnObj.put("errormsg", "传入参数错误");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}
		User currentUser=this.userService.getUserById(uid);
		JSONObject returnObj=new JSONObject();
		if(currentUser==null||currentUser.getStatus()!=1){
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", "当前用户不存在");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}else{
			returnObj.put("result", SUCCESS);
			User visitUser=this.userService.getUserById(vuid);
			JSONArray array=new JSONArray();
			Calendar now=Calendar.getInstance();
			Calendar firstDateOfMonth=Calendar.getInstance();
			firstDateOfMonth.clear(Calendar.MILLISECOND);
			firstDateOfMonth.clear(Calendar.SECOND);
			firstDateOfMonth.clear(Calendar.MINUTE);
			firstDateOfMonth.set(Calendar.HOUR_OF_DAY, 0);
			firstDateOfMonth.set(Calendar.YEAR, year);
			firstDateOfMonth.set(Calendar.MONTH,month);
			firstDateOfMonth.set(Calendar.DAY_OF_MONTH, 1);
			
			Calendar startdate=(Calendar) firstDateOfMonth.clone();
			startdate.add(Calendar.DAY_OF_MONTH, Calendar.SUNDAY-startdate.get(Calendar.DAY_OF_WEEK));
			
			Calendar lastDateOfMonth=(Calendar) firstDateOfMonth.clone();
			lastDateOfMonth.set(Calendar.DAY_OF_MONTH,firstDateOfMonth.getActualMaximum(Calendar.DAY_OF_MONTH));
			
			Calendar lastDate=(Calendar) lastDateOfMonth.clone();
			lastDate.add(Calendar.DAY_OF_MONTH, Calendar.SATURDAY-lastDate.get(Calendar.DAY_OF_WEEK));
			int holiday=0;
			while(!startdate.after(lastDate)){
				JSONObject object=new JSONObject();
				int isholiday=isSysHoliday(startdate.get(Calendar.YEAR), startdate.get(Calendar.MONTH), startdate.get(Calendar.DAY_OF_MONTH));
				int weeknum=startdate.get(Calendar.DAY_OF_WEEK);
				boolean isweekend=weeknum==1||weeknum==7?true:false;
				boolean isThisMonth=startdate.get(Calendar.MONTH)==this.month;
				
				Map<String,Object> temp2=new HashMap<String,Object>();
				object.put("isThisMonth",isThisMonth);
				long count=this.eventService.countShareEventsByDayAndUser(startdate, visitUser,currentUser.getId());
				object.put("eventsize", count);
				object.put("weeknum", startdate.get(Calendar.WEEK_OF_YEAR));
				object.put("weekofmonth", startdate.get(Calendar.WEEK_OF_MONTH));
				object.put("weekday", startdate.get(Calendar.DAY_OF_WEEK));
				object.put("lunartxt", Lunar.getLunarText(startdate));
				SimpleDateFormat dateFormat=new SimpleDateFormat("MMdd");
				object.put("date", dateFormat.format(startdate.getTime()));
				if(isweekend){
					if(isholiday==1||isholiday==-1){ //周末也是节日
						object.put("holiday", true);
					}else{ //周末不休息
						object.put("holiday",false);
					}
				}else{
					if(isholiday==1){ //平时但是节日
						object.put("holiday", true);
					}else{ //平时也不是节日
						object.put("holiday",false);
					}
				}
				startdate.add(Calendar.DAY_OF_MONTH, 1);
				array.add(object);
			}
			returnObj.put("month", array);
			
			Struts2Utils.renderJson(returnObj.toString());
		}
	}
	
	
	public void loadUsersShareByDay(){
		if(uid==0||month>11||month<0||year==0||vuid==0){
			JSONObject returnObj=new JSONObject();
			returnObj.put("result", PARAMERROR);
			returnObj.put("errormsg", "传入参数错误");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}
		User currentUser=this.userService.getUserById(uid);
		JSONObject returnObj=new JSONObject();
		if(currentUser==null||currentUser.getStatus()!=1){
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", "当前用户不存在");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}
		Calendar calendar=Calendar.getInstance();
		long totalcount=eventService.countsUserEventsByDay_FORAPP(year, month, date, currentUser.getId(), vuid);
		int perpagelength=0;
		if(pagelength!=null&&pagelength>0){
			perpagelength=this.pagelength;
		}else{
			perpagelength=this.CALENDAREVENTPAGELENGTH;
		}
		BigDecimal br = new BigDecimal(totalcount).divide(new BigDecimal(perpagelength),BigDecimal.ROUND_UP);
		Integer pageCount = (br.doubleValue()<1&&br.doubleValue()>=0 ? 1 : br.intValue());
		List events1=eventService.getUsersShareEventsByDay(this.year,this.month,this.date,this.vuid,currentUser,needpagenum,CALENDAREVENTPAGELENGTH);
		Calendar now=Calendar.getInstance();
		returnObj.put("result", SUCCESS);
		if(needpagenum>=pageCount-1){
			returnObj.put("hasmore", 0);
		}else{
			returnObj.put("hasmore", 1);
		}
		JSONArray array=new JSONArray();
		for (Object object : events1) {
			ClipEvent clipEvent=(ClipEvent) object;
			JSONObject temp=new JSONObject();
			temp.put("eventid", clipEvent.getId());
			temp.put("title", clipEvent.getTitle());
			temp.put("unameid", clipEvent.getUid());
			temp.put("uname", this.contactService.getUserName(currentUser.getId(), clipEvent.getUid()));
			List logolist=this.userService.getUserLogoByUid(clipEvent.getUid());
			temp.put("unamebiglogo", logolist.get(0));
			temp.put("unamesmalllogo", logolist.get(1));
			temp.put("reliedclip", clipEvent.getReliedclip());
			temp.put("starttime", clipEvent.getBegintime());
			temp.put("endtime", clipEvent.getEndtime());
			temp.put("iswholeday", clipEvent.getIswholeday());
			array.add(temp);
		}
		returnObj.put("events", array);
		Struts2Utils.renderJson(returnObj.toString());
	}
	
	public void getSubscribeEventList(){
		if(uid==0||ccuid==0){
			JSONObject returnObj=new JSONObject();
			returnObj.put("result", PARAMERROR);
			returnObj.put("errormsg", "传入参数错误");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}
		User currentUser=this.userService.getUserById(uid);
		JSONObject returnObj=new JSONObject();
		if(currentUser==null||currentUser.getStatus()!=1){
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", "当前用户不存在");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}
		List events1=this.eventService.getSubscribeEventList(ccuid,needpagenum,SUBSCRIBEEVENTLISTLENGTH);
		Calendar now=Calendar.getInstance();
		returnObj.put("result", SUCCESS);
		JSONArray array=new JSONArray();
		for (Object object : events1) {
			ClipEvent clipEvent=(ClipEvent) object;
			JSONObject temp=new JSONObject();
			temp.put("eventid", clipEvent.getId());
			temp.put("title", clipEvent.getTitle());
			temp.put("name", this.contactService.getUserName(currentUser.getId(), clipEvent.getUid()));
			List logolist=this.userService.getUserLogoByUid(clipEvent.getUid());
			temp.put("smalllogo", logolist.get(1));
			temp.put("reliedclip", clipEvent.getReliedclip());
			temp.put("starttime", clipEvent.getBegintime());
			temp.put("endtime", clipEvent.getEndtime());
			temp.put("iswholeday", clipEvent.getIswholeday());
			array.add(temp);
		}
		returnObj.put("events", array);
		Struts2Utils.renderJson(returnObj.toString());
	}
	
	public void messageWithClipevent(){
		if(uid==0||eventid==0||acceptstatus!=1||acceptstatus!=0){
			JSONObject returnObj=new JSONObject();
			returnObj.put("result", PARAMERROR);
			returnObj.put("errormsg", "传入参数错误");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}
		User currentUser=this.userService.getUserById(uid);
		JSONObject returnObj=new JSONObject();
		if(currentUser==null||currentUser.getStatus()!=1){
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", "当前用户不存在");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}
		String re=this.eventService.controlMessageWithClipevent(currentUser.getId(),eventid,acceptstatus);
		if(re==null){
			returnObj.put("result", SUCCESS);
		}else{
			returnObj.put("result", FAILED);
			returnObj.put("errormsg",re);
		}
		Struts2Utils.renderJson(returnObj.toString());
	}
	
	public void addNewInvitesWithShareEvent(){
		JSONObject returnObj=new JSONObject();
		if(uid==0||eventid==0||share2userlist==null||share2userlist.length()==0){
			returnObj.put("result", PARAMERROR);
			returnObj.put("errormsg", "传入参数错误");
			Struts2Utils.renderJson(returnObj.toString());
			return;
		}
		User currentUser=this.userService.getUserById(uid);
		try {
			eventService.forwardMoreWithShareEvent(eventid,currentUser,share2userlist,"");
			returnObj.put("result", SUCCESS);
		} catch (ClipEventNotExistException e) {
			returnObj.put("result", FAILED);
			returnObj.put("errormsg", e.getErrorMessage());
		}
		Struts2Utils.renderJson(returnObj.toString());
	}
	
	
	
	private int eventid;
	private int cliptype;
	private String title;
	private String address;
	private String content;
	private long begintime;
	private long endtime;
	private int iswholeday;
	private long alerttime;
	private int mobilenotice;
	private long deadlinetime;
	private String contactlist;
	private String sharelist;
	private String share2userlist;
	private int uid;
	private float longitude;
	private float latitude;
	private String md5code;
	private File clippic;
	private String clippicfilename;
	
	private int needpagenum;
	private Integer pagelength;
	private String searchkey;
	private String comment;
	
	private int year;
	private int month;
	private int date;
	private String querytype;
	private String ids;
	private int isfavor;
	private int seid;
	private int isLoadSSAll=1;
	private int ccuid;
	private int vuid;
	private int acceptstatus;
	
	private long invitetime;
	public int getEventid() {
		return eventid;
	}
	public void setEventid(int eventid) {
		this.eventid = eventid;
	}
	public int getCliptype() {
		return cliptype;
	}
	public void setCliptype(int cliptype) {
		this.cliptype = cliptype;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public long getBegintime() {
		return begintime;
	}
	public void setBegintime(long begintime) {
		this.begintime = begintime;
	}
	public long getEndtime() {
		return endtime;
	}
	public void setEndtime(long endtime) {
		this.endtime = endtime;
	}
	public int getIswholeday() {
		return iswholeday;
	}
	public void setIswholeday(int iswholeday) {
		this.iswholeday = iswholeday;
	}
	public long getAlerttime() {
		return alerttime;
	}
	public void setAlerttime(long alerttime) {
		this.alerttime = alerttime;
	}
	public int getMobilenotice() {
		return mobilenotice;
	}
	public void setMobilenotice(int mobilenotice) {
		this.mobilenotice = mobilenotice;
	}
	public long getDeadlinetime() {
		return deadlinetime;
	}
	public void setDeadlinetime(long deadlinetime) {
		this.deadlinetime = deadlinetime;
	}
	public String getContactlist() {
		return contactlist;
	}
	public void setContactlist(String contactlist) {
		this.contactlist = contactlist;
	}
	public String getSharelist() {
		return sharelist;
	}
	public void setSharelist(String sharelist) {
		this.sharelist = sharelist;
	}
	public String getShare2userlist() {
		return share2userlist;
	}
	public void setShare2userlist(String share2userlist) {
		this.share2userlist = share2userlist;
	}
	public int getUid() {
		return uid;
	}
	public void setUid(int uid) {
		this.uid = uid;
	}
	public float getLongitude() {
		return longitude;
	}
	public void setLongitude(float longitude) {
		this.longitude = longitude;
	}
	public float getLatitude() {
		return latitude;
	}
	public void setLatitude(float latitude) {
		this.latitude = latitude;
	}
	public String getMd5code() {
		return md5code;
	}
	public void setMd5code(String md5code) {
		this.md5code = md5code;
	}
	public File getClippic() {
		return clippic;
	}
	public void setClippic(File clippic) {
		this.clippic = clippic;
	}
	public String getClippicfilename() {
		return clippicfilename;
	}
	public void setClippicfilename(String clippicfilename) {
		this.clippicfilename = clippicfilename;
	}

	public int getNeedpagenum() {
		return needpagenum;
	}

	public void setNeedpagenum(int needpagenum) {
		this.needpagenum = needpagenum;
	}

	public String getSearchkey() {
		return searchkey;
	}

	public void setSearchkey(String searchkey) {
		this.searchkey = searchkey;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public int getDate() {
		return date;
	}

	public void setDate(int date) {
		this.date = date;
	}

	public String getQuerytype() {
		return querytype;
	}

	public void setQuerytype(String querytype) {
		this.querytype = querytype;
	}

	public int getSeid() {
		return seid;
	}

	public void setSeid(int seid) {
		this.seid = seid;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public int getIsfavor() {
		return isfavor;
	}

	public void setIsfavor(int isfavor) {
		this.isfavor = isfavor;
	}

//	public int getIsLoadSSAll() {
//		return isLoadSSAll;
//	}
//
//	public void setIsLoadSSAll(int isLoadSSAll) {
//		this.isLoadSSAll = isLoadSSAll;
//	}

	public int getCcuid() {
		return ccuid;
	}

	public void setCcuid(int ccuid) {
		this.ccuid = ccuid;
	}

	public int getVuid() {
		return vuid;
	}

	public void setVuid(int vuid) {
		this.vuid = vuid;
	}

	public int getAcceptstatus() {
		return acceptstatus;
	}

	public void setAcceptstatus(int acceptstatus) {
		this.acceptstatus = acceptstatus;
	}

	public long getInvitetime() {
		return invitetime;
	}

	public void setInvitetime(long invitetime) {
		this.invitetime = invitetime;
	}

	public Integer getPagelength() {
		return pagelength;
	}

	public void setPagelength(Integer pagelength) {
		this.pagelength = pagelength;
	}
	
	
	
}
